package com.tarena.lbs.basic.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.repository.AdminRepository;
import com.tarena.lbs.basic.web.repository.UserGroupRepository;
import com.tarena.lbs.basic.web.repository.UserTagsRepository;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.po.UserGroupPO;
import com.tarena.lbs.pojo.basic.po.UserTagsPO;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserGroupService {
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private UserTagsRepository userTagsRepository;
    public PageResult<UserGroupVO> userGroupList(UserGroupQuery query) throws BusinessException {
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证失败"));
        AdminPO adminPO = adminRepository.getAdminById(userPrinciple.getId());
        Asserts.isTrue(adminPO==null,new BusinessException("-2","商家不存在"));
        PageInfo<UserGroupPO> pageInfo = userGroupRepository.getUserGroupsByPage(adminPO.getBusinessId());
        PageResult<UserGroupVO> voPages=new PageResult<>();
        voPages.setPageNo(pageInfo.getPageNum());
        voPages.setPageSize(pageInfo.getPageSize());
        voPages.setTotal(pageInfo.getTotal());
        List<UserGroupVO> vos=null;
        if (pageInfo.getTotal()>0){
            vos=pageInfo.getList().stream().map(po->{
                UserGroupVO vo=new UserGroupVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPages.setObjects(vos);
        return voPages;
    }

    public void userGroupSave(UserGroupParam param) throws BusinessException {
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证失败"));
        AdminPO adminPO = adminRepository.getAdminById(userPrinciple.getId());
        Asserts.isTrue(adminPO==null,new BusinessException("-2","商家不存在"));
        UserGroupPO poParam=new UserGroupPO();
        BeanUtils.copyProperties(param,poParam);
        poParam.setBusinessId(adminPO.getBusinessId());
        poParam.setCreateAt(new Date());
        userGroupRepository.saveAdmin(poParam);
    }

    public boolean isUserGroup(Integer userId, Integer userGroupId) {
        //user实体 userTags实体  userGroup实体
        //1.userId可以从 lbs_user_tags 查询用户绑定的标签 包含标签id
        List<UserTagsPO> userTagsPos=userTagsRepository.getUserTags(userId);
        log.info("当前用户:{},绑定的标签:{}",userId,userTagsPos);
        //2.userGroupId 查询关联的所有标签 String(1,5,9)
        UserGroupPO userGroupPo=userGroupRepository.getUserGroup(userGroupId);
        log.info("当前查询用户的人群:{}",userGroupPo);
        if (CollectionUtils.isNotEmpty(userTagsPos)&&userGroupPo!=null){
            //先将用户人群 tagIds做切分
            String[] tagIds = userGroupPo.getTagIds().split(",");
            //只要有一个标签是交集 返回结果就是true
            for (String tagId : tagIds) {
                for (UserTagsPO po:userTagsPos){
                    if (po.getTagId().equals(Integer.valueOf(tagId))){
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
